package edu.mis.modules.customer;

import java.util.Date;

import com.jfinal.core.Controller;

import edu.mis.model.Customer;
import edu.mis.model.Member;
import edu.mis.modules.member.MemberService;
import edu.mis.modules.user.UserService;

/**
 * 日志控制器
 *
 */
public class CustomerController extends Controller {
	
	static CustomerService service = new CustomerService();
	static UserService userService = new UserService();
	static MemberService memberService = new MemberService();
	
	/**
	 * 日志列表视图页面
	 */
	public void index() {   //如果为index，则访问地址为"路由名/" ,此处为"customer/"
		if(userService.findUserWithRole().equals(1)) {
			render("customer.html");
		}else if(userService.findUserWithRole().equals(2)){
			render("cashierCustomer.html");
		}else {
			render("noAllAuthority.html");
		}
		
	}
	
	/**
	 * 日志列表后台json
	 */
	
	public void list(){   //访问地址为 customer/list
		
		setAttr("data", service.findAllCustomerWithContionName());
		renderJson();   //可直接在浏览器输入"localhost:8080/customer/list"查看返回的Json值，可以作为调试的方法
	}
	
	
	/**
	 * 添加客户视图
	 */
	public void add() {
		int customerNumber = service.ramdomCustomerNumber();
		setAttr("customerNumber", customerNumber);
	}

	/**
	 * 添加客户
	 */
	public void save(){		
		Customer customer  =  getModel(Customer.class); 
		customer.setVip(2);
		if(customer.getCustomerSex()==null) {
			customer.setCustomerSex("男");
		}
		
		if(customer.getCustomerName()!=null) {
			if(customer.getCustomerPhone()!=null) {
				if(service.saveCustomer(customer)){
					setAttr("result", true); 
					setAttr("msg", "添加客户成功!"); 
				}else{
					setAttr("result",false);
					setAttr("msg", "添加客户失败，客户编号重复!");
				}
				;
			}else {
				setAttr("result",false);
				setAttr("msg", "添加客户失败，客户号码为空!");
			}
		}else {
			setAttr("result",false);
			setAttr("msg", "添加客户失败，客户姓名为空!");
		}
		renderJson();  //返回json，用于向前台页面返回结果
	}
	
	public void edit() {
		setAttr("customer", service.findCustomerByCustomerId(getParaToInt("customerId")));
	}
	
	/**
	 * 修改日志
	 */
	public void update(){
		Customer customer  =  getModel(Customer.class);
		
		if(customer.getCustomerName()!=null) {
			if(customer.getCustomerPhone()!=null) {
				if(service.updateCustomer(customer)){
					setAttr("result", true);
					setAttr("msg", "修改客户成功!");
				}else{
					setAttr("result", false);
					setAttr("msg", "修改客户失败!");
				}
			}else {
				setAttr("result", false);
				setAttr("msg", "修改客户失败，客户电话为空!");
			}
		}else {
			setAttr("result", false);
			setAttr("msg", "修改客户失败，客户姓名为空!");
		}
		renderJson();
	}
	
	/**
	 * 办理会员视图
	 */
	public void auth() {
		int cost = 300;
		setAttr("cost", cost);
		setAttr("login",userService.findLoginUser());
		setAttr("customer", service.findCustomerByCustomerId(getParaToInt("customerId")));
	}
	
	//添加会员
	public void authorize(){
		Customer customer = getModel(Customer.class);
		Member member = getModel(Member.class);
		member.setTime(new Date());
		member.setMemberScore(50);
		member.setUserId(userService.findLoginUser().getUserId());
		
		
		if(service.findCustomerByCustomerId(customer.getCustomerId()).getVip()==2) {
			if(memberService.saveMember(member)) {
				customer = service.findCustomerByCustomerId(customer.getCustomerId());
				customer.setVip(1);
				if(service.updateCustomer(customer)) {
					setAttr("result", true);
			        setAttr("msg", "办理会员成功!");
				}else {
					setAttr("result", false);
				    setAttr("msg", "办理会员失败，系统出错!");
				}
		    }else {
			    setAttr("result", false);
			    setAttr("msg", "办理会员失败，该客户已经是会员了!");
		    }
		}else {
			 setAttr("result", false);
			 setAttr("msg", "办理会员失败，该客户已经是会员了!");
		}
		
		renderJson();
	}
	
	/**
	 * 删除客户
	 * 测试访问地址：localhost:8080/customer/delete?id=1
	 */
	public void delete() {		
		if(service.deleteCustomerByCustomerId(getParaToInt("customerId"))){
			setAttr("result", true);
			setAttr("msg", "删除客户成功!");
		}else{
			setAttr("result", false);
		}
		renderJson();
	}
}


